#include "spinlock.h"

#include <assert.h>


namespace tinySrv
{  
    
    Spinlock::Spinlock()
      : SpinHandle(pthread_spinlock_t())
    {
      if (pthread_spin_init(&SpinHandle, PTHREAD_PROCESS_PRIVATE))
        throw SpinlockException("Can't create spilock object");
    }
    
    Spinlock::~Spinlock()
    {
      int Result = pthread_spin_destroy(&SpinHandle);
      assert(Result == 0 && "pthread_spin_destroy failed");
    }
    
    void Spinlock::Lock()
    {
      if (pthread_spin_lock(&SpinHandle))
        throw SpinlockException("Failed to lock");
    }
    
    void Spinlock::Unlock()
    {
      if (pthread_spin_unlock(&SpinHandle))
        throw SpinlockException("Failed to unlock");
    }

}
